

package ca.jsbr.as2js_lib.display.animations 
{
	import ca.jsbr.as2js_lib.display.animations.easing.Linear;
	import ca.jsbr.as2js_lib.display.canvas.CanvasStage;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	import flash.utils.setTimeout;
	import js.window;
	import jslib.jslib;
	/**
	 * ...
	 * @author jsbr [jsricard@lvlstudio.com]
	 */
	public class Tween
	{
		private var _interval:int;
		private var _duration:Number;
		private var _time:Number;
		private var _startTime:Number;
		private var _tweens:Object;
		private var _obj:Object={};
		private var _start:Object={};
		private var _ease:Object={};
		private var _onComplete:Function;
		private var _onCompleteParam:Function;
		private var _delay:Number;
		public function Tween(obj:Boolean, duration:Number, tween:Object)
		{
			_duration = duration * 1000;
			
			
			_time=0;
			
			_tweens=tween;
			_obj=obj;
			_start = new Object();
			_ease=null;
			
			_onComplete=null;
			_onCompleteParam=null;
			
			
			for (var ind in _tweens)
			{
				switch(ind)
				{
					case "ease":
						_ease=_tweens[ind]
						delete _tweens["ease"];
						break;
					case "onComplete":
						_onComplete=_tweens[ind]
						delete _tweens["onComplete"];
						break;
					case "delay":
						_delay=_tweens[ind]
						delete _tweens["delay"];
						break;
					case "onCompleteParam":
						_onCompleteParam=_tweens[ind]
						delete _tweens["onCompleteParam"];
						break;
					default:
						_start[ind]=_obj[ind];
						break;
				}
				
			}
			if(!_ease)
				_ease = Linear.easeNone;
			if (_delay)
				setTimeout(start, _delay * 1000);
			else
				start();
		}
		
		private function start():void
		{
			var d:Date = new Date();
			_startTime = d.getTime();
			_interval = setInterval(render,1000/CanvasStage.instance.stageDescription.fps);
		}
		
		static public function to(obj:Object, duration:Number, tween:Object)
		{
			if(obj.__$tween__)
				obj.__$tween__.stop();
			obj.__$tween__ = new Tween(obj, duration, tween);
		}

		
		/**
		 * Stop tween immediately
		 */
		public function stop():void
		{
			clearInterval(_interval);
		}
		
		/**
		 * @private
		 */
		public function render():void
		{
			var d:Date = new Date();
			_time = d.getTime() - _startTime;
			if(_time>=_duration)
			{
				for (var ind in _tweens)
				{
					var val=_tweens[ind];
					_obj[ind]=val;
					//_obj[ind]=_ease(_time, _start[ind], _tweens[ind]-_start[ind], _duration);
				}
				if(_onComplete)
				{
					if(_onCompleteParam)
						_onComplete.apply(window,_onCompleteParam);
					else
						_onComplete();
				}
				clearInterval(_interval);
			}
			else
			{
				for (var ind in _tweens)
				{
					var val = _ease(_time, _start[ind], _tweens[ind] - _start[ind], _duration);
					if(val>_tweens[ind] && _tweens[ind]-_start[ind]>0)
						val=_tweens[ind];
					else if(val<_tweens[ind] && _tweens[ind]-_start[ind]<0)
						val=_tweens[ind];
					_obj[ind]=val;
					//_obj[ind]=_ease(_time, _start[ind], _tweens[ind]-_start[ind], _duration);
				}
			}
		}
	}
}